package com.pinyoumei.app.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.Photo;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2020-10-22.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-10-22        Administrator             新增Person类
 * 2020-10-22        Administrator             增加sex属性
 */
public class MallDetailImageAdapter extends BaseQuickAdapter<Photo, BaseViewHolder> {

    private RequestOptions placeholder;

    public MallDetailImageAdapter(int layoutResId, @Nullable List<Photo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Photo item) {
       ImageView imageView = helper.getView(R.id.iv_bg);
        placeholder = new RequestOptions().placeholder(R.mipmap.ic_error_loading);



        if (item.getUrl().contains(".gif")){
            Glide.with(mContext)
                    .load(item.getUrl())
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .apply(placeholder)
                    //.thumbnail(Glide.with(this).load(R.mipmap.ic_launcher))
                    .into(imageView);
        }else{

//            Glide.with(mContext).load(item.getUrl()).apply(placeholder).into(imageView);

        Glide.with(mContext).asBitmap().load(item.getUrl()).apply(placeholder).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);

                //获取原图的宽高
                int width = resource.getWidth();
                int height = resource.getHeight();

                //获取imageView的宽
                int imageViewWidth = imageView.getWidth();

                //计算缩放比例
                float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1);

                //计算图片等比例放大后的高
                int imageViewHeight = (int) (height * sy);
                ViewGroup.LayoutParams params = imageView.getLayoutParams();
                params.height = imageViewHeight;
                imageView.setLayoutParams(params);
            }
        });
    }

    }

}
